<?php
/**
 * MyValidator.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/10/21- 12:00 下午
 */
namespace common\helpers;
use common\components\ext\ValidatorModelExt;
use common\exception\ValidatorException;
use yii\base\Component;

class MyValidator extends Component
{
    public function init()
    {
        parent::init();
    }

    /**
     * 校验参数
     * @param array $data
     * @param array $rules
     * @param array $attributeLabels
     * @throws ValidatorException
     * @throws \yii\base\InvalidConfigException
     */
    public function validate(array  &$data, array $rules,array $attributeLabels = [])
    {
        if (!empty($rules)) {
            $model = ValidatorModelExt::validateData($data, $rules, $attributeLabels);
            if ($model->hasErrors()) {
                $error = $model->getErrorSummary(false);
                throw new ValidatorException(reset($error));
            }
        }

    }
}